package com.whxy02.tolearn.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whxy02.tolearn.app.beans.QuestionCollection;
import com.whxy02.tolearn.app.dao.QuestionCollectionDao;
import com.whxy02.tolearn.app.service.QuestionCollectionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class QuestionCollectionServiceImpl implements QuestionCollectionService {
    private QueryWrapper<QuestionCollection> wrapper = new QueryWrapper<>();

    @Resource
    QuestionCollectionDao questionCollectionDao;

    @Override
    public List<QuestionCollection> getAll() throws Exception {
        return questionCollectionDao.selectList(null);
    }

    @Override
    public QuestionCollection getById(Integer id) throws Exception {
        return questionCollectionDao.selectById(id);
    }

    @Override
    public int save(QuestionCollection questionCollection) throws Exception {
        return questionCollectionDao.insert(questionCollection);
    }

    @Override
    public int removeById(Integer id) throws Exception {
        return questionCollectionDao.deleteById(id);
    }

    @Override
    public int change(QuestionCollection questionCollection) throws Exception {
        return questionCollectionDao.updateById(questionCollection);
    }

    @Override
    public QuestionCollection getByUserAndQuestion(Integer userId, Integer questionId) throws Exception {
        wrapper.clear();
        wrapper.eq("user_id", userId).eq("question_id", questionId);
        return questionCollectionDao.selectOne(wrapper);
    }

    @Override
    public Page<QuestionCollection> getByUserIdAndDateDescPage(Integer userId,int nowPage) throws Exception {
        wrapper.clear();
        wrapper.eq("user_id",userId).orderByDesc("create_time");
        Page<QuestionCollection> page=new Page<>(nowPage,20);
        return questionCollectionDao.selectPage(page,wrapper);
    }
}
